#!/bin/bash

########################
#
# 配置Hadoop的其他配置文件
#
#######################

# 设置 Hadoop 的安装路径
HADOOP_INSTALL_DIR="/opt/hadoop"
HADOOP_DIR=$(basename $(ls -d /opt/hadoop/hadoop-*))

# 配置 hadoop-env.sh
echo "配置 hadoop-env.sh..."
sudo sed -i '/^# The java implementation to use./a export JAVA_HOME=/opt/java/jdk1.8.0_152' $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/hadoop-env.sh

# 配置 core-site.xml
echo "配置 core-site.xml..."
cat <<EOL | sudo tee $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/core-site.xml
<configuration>
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:9000</value>
</property>
</configuration>
EOL

# 配置 hdfs-site.xml
echo "配置 hdfs-site.xml..."
cat <<EOL | sudo tee $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/hdfs-site.xml
<configuration>
<property>
<name>dfs.name.dir</name>
<value>/home/cc/hadoop/name</value>
</property>
<property>
<name>dfs.data.dir</name>
<value>/home/cc/hadoop/data</value>
</property>
<property>
    <name>dfs.namenode.rpc-address</name>
    <value>localhost:9000</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
    <name>dfs.namenode.http-address</name>
    <value>0.0.0.0:9870</value>
</property>
</configuration>
EOL

# 配置 mapred-site.xml
echo "配置 mapred-site.xml..."
sudo cp $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/mapred-site.xml.template $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/mapred-site.xml
cat <<EOL | sudo tee $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
EOL

# 配置 yarn-site.xml
echo "配置 yarn-site.xml..."
cat <<EOL | sudo tee $HADOOP_INSTALL_DIR/$HADOOP_DIR/etc/hadoop/yarn-site.xml
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>
EOL

echo "Hadoop 配置文件已配置完成。"